package com.starbucks.opencase.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.starbucks.opencase.entity.entGoodsInfo;
import com.starbucks.opencase.enums.enumCMKey;
import com.starbucks.opencase.enums.enumCMTip;
import com.starbucks.opencase.function.funTest;
import com.starbucks.opencase.request.reqData;
import com.starbucks.opencase.tool.toolAdminAccount;
import com.starbucks.opencase.tool.toolGoodsInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/goods")
public class conAdminGoods {
    @Autowired
    private funTest funTest;
    @Autowired
    private toolAdminAccount toolAdminAccount;
    @Autowired
    private toolGoodsInfo toolGoodsInfo;
    @PostMapping("/query/list")
    public String queryList(@RequestBody reqData info){
        try{
            ObjectMapper om = new ObjectMapper();
            JsonNode me = om.readTree(info.getData());
            ObjectNode on=om.createObjectNode();
            on.put(enumCMKey.timestamp.name(),System.currentTimeMillis());
            on.put(enumCMKey.status.name(), false);
            if(!funTest.hasPhone(me,on))return on.toString();
            if(!funTest.hasCommand(me,on))return on.toString();
            String phone= funTest.getPhone(me);
            String command= funTest.getCommand(me);
            if(!toolAdminAccount.queryCommand(phone).equals(command)){
                on.put(enumCMKey.tip.name(), enumCMTip.error_command.getTip());
                return on.toString();
            }
            if(!funTest.hasName(me,on))return on.toString();
            if(!funTest.hasNowPageNum(me,on))return on.toString();
            String name= funTest.getName(me);
            int num= funTest.getNowPageNum(me);
            on.put(enumCMKey.nowPageNum.name(), num);
            on.put(enumCMKey.allPageNum.name(), toolGoodsInfo.queryAllPageNum(name));
            List<entGoodsInfo> as=toolGoodsInfo.queryList(name,num);
            ArrayNode an=om.createArrayNode();
            for(entGoodsInfo a:as){
                ObjectNode o=om.createObjectNode();
                o.put(enumCMKey.gid.name(), a.getGid());
                o.put(enumCMKey.name.name(),a.getName());
                o.put(enumCMKey.price.name(),a.getPrice());
                o.put(enumCMKey.useTime.name(), a.getUseTime());
                an.add(o);
            }
            on.put(enumCMKey.data.name(), an);
            on.put(enumCMKey.status.name(), true);
            return on.toString();
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
